/*
 * Created on 2008-06-04
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package elka.isi.operator;

import org.evolvica.core.IIndividual;
import org.evolvica.core.IIndividualComparator;
import org.evolvica.core.IIndividualSet;
import org.evolvica.core.ISetIterator;
import org.evolvica.core.IndividualArray;
import org.evolvica.core.common.AbstractGeneticOperator;
import org.evolvica.core.population.IInsertionFilter;
import org.evolvica.core.population.IPopulation;
import org.evolvica.core.population.SortedPopulation;

import elka.isi.gene.*;

/**
 * @author Zibi
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public abstract class SortedEcosystem extends AbstractGeneticOperator implements IPopulation {

	protected int wolfApetite;

	protected int generation = 0;
	protected IIndividualComparator comparator;
	protected SortedPopulation wolvesPopulation = new SortedPopulation();
	protected SortedPopulation rabbitsPopulation = new SortedPopulation();
	
	/* (non-Javadoc)
	 * @see org.evolvica.core.population.IPopulation#insert(org.evolvica.core.IIndividualSet)
	 */
	public abstract IIndividualSet insert(IIndividualSet iset); 
	

	/* (non-Javadoc)
	 * @see org.evolvica.core.population.IPopulation#replace(org.evolvica.core.IIndividual, org.evolvica.core.IIndividual)
	 */
	public void replace(IIndividual ind, IIndividual subst) {
		throw new UnsupportedOperationException();
		
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.population.IPopulation#replace(org.evolvica.core.IIndividual[], org.evolvica.core.IIndividual[])
	 */
	public void replace(IIndividual[] indArray, IIndividual[] substArray) {
		throw new UnsupportedOperationException();
		
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.population.IPopulation#setFilter(org.evolvica.core.population.IInsertionFilter)
	 */
	public void setFilter(IInsertionFilter filter) {
		wolvesPopulation.setFilter(filter);
		rabbitsPopulation.setFilter(filter);
	}

	public void setWolvesFilter(IInsertionFilter filter) {
		wolvesPopulation.setFilter(filter);
		
	}
	public void setRabbitsFilter(IInsertionFilter filter) {
		rabbitsPopulation.setFilter(filter);
		
	}
	/* (non-Javadoc)
	 * @see org.evolvica.core.IIndividualSet#add(org.evolvica.core.IIndividual)
	 */
	public void add(IIndividual ind) {
		if (ind.getGenotype() instanceof RabbitGene) 
			rabbitsPopulation.add(ind);
		else if (ind.getGenotype() instanceof WolfGene)
			wolvesPopulation.add(ind);
		else throw new IllegalArgumentException("Wrong genotype!");	
		
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.IIndividualSet#add(org.evolvica.core.IIndividual[])
	 */
	public void add(IIndividual[] indArray) {
		throw new UnsupportedOperationException();
		
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.IIndividualSet#clear()
	 */
	public void clear() {
		wolvesPopulation.clear();
		rabbitsPopulation.clear();
		
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.IIndividualSet#contains(org.evolvica.core.IIndividual)
	 */
	public boolean contains(IIndividual ind) {
		throw new UnsupportedOperationException();
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.IIndividualSet#isEmpty()
	 */
	public boolean isEmpty() {
		throw new UnsupportedOperationException();
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.IIndividualSet#iterator()
	 */
	public ISetIterator iterator() {
		throw new UnsupportedOperationException();
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.IIndividualSet#remove(org.evolvica.core.IIndividual)
	 */
	public void remove(IIndividual ind) {
		throw new UnsupportedOperationException();
		
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.IIndividualSet#remove(org.evolvica.core.IIndividual[])
	 */
	public void remove(IIndividual[] indArray) {
		throw new UnsupportedOperationException();
		
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.IIndividualSet#size()
	 */
	public int size() {
		
		return wolvesPopulation.size() + rabbitsPopulation.size();
	}

	/* (non-Javadoc)
	 * @see org.evolvica.core.IIndividualSet#toArray()
	 */
	public IIndividual[] toArray() {
		IIndividualSet all = new IndividualArray();
		all.add(wolvesPopulation.toArray());
		all.add(rabbitsPopulation.toArray());
		return all.toArray();
	}

	/* (non-Javadoc)
	 * @see org.evolvica.engine.IProcessor#process(java.lang.Object)
	 */
	public Object process(Object input) {
		return insert( (IIndividualSet) input );
	}
	
	public void setComparator(IIndividualComparator comparator){
		this.comparator = comparator;
		wolvesPopulation.setComparator(comparator);
		rabbitsPopulation.setComparator(comparator);
	}

	/**
	 * @param i
	 */
	public void setWolfApetite(int i) {
		wolfApetite = i;
	}

	/**
	 * @return
	 */
	public SortedPopulation getRabbitsPopulation() {
		return rabbitsPopulation;
	}

	/**
	 * @return
	 */
	public SortedPopulation getWolvesPopulation() {
		return wolvesPopulation;
	}

}
